import { createApp } from 'vue';
import { createPinia } from 'pinia';
import 'amfe-flexible';
import { getDeviceId, reportEvent } from '@utils/NativeUtils';
import routes from '@/routes/index';
import i18n from '@/lang/i18nSetup';
import App from '@/App.vue';
import '@icon-park/vue-next/styles/index.css';
import '@/assets/styles/animate.min.css';
import VantInstall from '@plugins/vant';
// import '@plugins/vConsole';
import CustomDirectives from '@utils/CustomDirectives';
import BrowserLogger from 'alife-logger';
import { getBrowserLoggerConfig, queryStringParse } from './utils/common';

routes.beforeEach((to) => {
    // 注入全局变量
    if (app) {
        app.provide('$isRtl', i18n.global?.['isRtl']?.value);
        app.provide(
            '$textAlign',
            `text-align: ${i18n.global?.['isRtl']?.value ? 'right' : 'left'};`
        ); // 文字对齐方式
        app.provide('$langDirection', i18n.global?.['direction']?.value); // 文本书写顺序
        app.provide(
            '$rowReverse',
            i18n.global?.['isRtl']?.value ? 'flex-direction: row-reverse;' : ''
        ); // 页面元素排列方式
    }
    setTimeout(() => {
        reportEvent(
            'H5页面加载完成',
            'page_loading_succeeded',
            `耗时:${window.sessionStorage.getItem('visitTime')}ms;page_path:${to.path}`
        );
    }, 2000);
});

// TODO:暂时注释
// getDeviceId()
//     .then((deviceId) => {
//         window.localStorage.setItem('deviceId', deviceId.toString());
//     })
//     .catch(() => {
//         console.info('get device id failed');
//     });

const app = createApp(App).use(routes).use(i18n).use(createPinia());
VantInstall(app);
// 注册全局自定义指令
Object.entries(CustomDirectives).forEach(([key, value]) => {
    app.directive(key, value);
});
app.mount('#app');

// 引入arms监控
const __bl = BrowserLogger.singleton(getBrowserLoggerConfig());

// arms监控设置用户名
window.onload = () => {
    const query = queryStringParse(window.location.search);
    __bl.setConfig({
        setUsername() {
            return query.token || null;
        },
    });
};
